package com.hotai.controller;

import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.yanzheng.RandomUtil;
import com.hotai.service.UserStudentSerivice;
import com.hotai.vo.courseware.CoursewaredetaileList;
import com.hotai.vo.she.SheBao;
import com.hotai.vo.user.LoginDto;
import com.hotai.vo.user.LoginZhu;
import com.hotai.vo.userteacher.StuXiuGai;
import com.hotai.vo.userteacher.TeaXiuGai;
import com.hotai.vo.usestudent.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/StudentUser")
@CrossOrigin
public class UserStudentController {

    @Autowired
    private UserStudentSerivice userStudentSerivice;

//
//    @Autowired
//    private MsmService msmService;



    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //设备登录
    @PostMapping("/loginUser")
    public ResponseResult login(@RequestBody LoginDto dto) {
        return userStudentSerivice.login(dto);
    }

    //公众号
    @PostMapping("/loUser")
    public ResponseResult paUser(@RequestBody LoginDto dto) {
        return userStudentSerivice.Palogin(dto);
    }

    //设备端注册
    @PostMapping("/register")
    public ResponseResult registe(@RequestBody  @Validated LoginZhu loginZhu) {
        return userStudentSerivice.zhuCe(loginZhu);
    }

    //短息验证码
    @RequestMapping("/send/{phone}")
    public ResponseResult sendMsm(@PathVariable("phone")  String phone){
        //1. 从redis中获取验证码，如果有直接返回
       //删除
        redisTemplate.delete(phone);
        String code = redisTemplate.opsForValue().get(phone);

//        if(!StringUtils.isEmpty(code)){
//            return ResponseResult.okResult(code);
//        }
        //如果没有就向阿里云发送请求
        //生成随机值，传递阿里云进行发送
        code= RandomUtil.getSixBitRandom();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        boolean isSend= userStudentSerivice.send(map,phone);
        if(isSend){
            //发送成功 把发送成功验证码放到redis里面
            //设置有效时间
            redisTemplate.opsForValue().set(phone,code,10, TimeUnit.MINUTES);
            return ResponseResult.okResult(map);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
        }

    }

    //短息验证码管理端
    @RequestMapping("/sendM")
    public ResponseResult sendMs(@Param("phone")  String phone){
        //1. 从redis中获取验证码，如果有直接返回
        redisTemplate.delete(phone);
        String code = redisTemplate.opsForValue().get(phone);

//        if(!StringUtils.isEmpty(code)){
//            return ResponseResult.okResult(code);
//        }
        //如果没有就向阿里云发送请求
        //生成随机值，传递阿里云进行发送
        code= RandomUtil.getSixBitRandom();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        boolean isSend= userStudentSerivice.send(map,phone);
        if(isSend){
            //发送成功 把发送成功验证码放到redis里面
            //设置有效时间
            redisTemplate.opsForValue().set(phone,code,10, TimeUnit.MINUTES);
            return ResponseResult.okResult(map);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
        }

    }

    //设备端修改密码
    @RequestMapping("/Shechong")
    private ResponseResult UserStudentSheChong(@RequestBody SheChon sheChon) {
        return userStudentSerivice.UserSheChong(sheChon);
    }



//管理端
    @RequestMapping("/list")
    private ResponseResult UserStudentList(UserStudentList query) {
        return userStudentSerivice.UserStudentPage(query);
    }

//管理端 用户详情
    @RequestMapping("/detail")
    private ResponseResult Coursewaredetail(UserStudentdetaile queryware) {
        return userStudentSerivice.UserStudentdetail(queryware);
    }


    //设备端重置
    @RequestMapping("/chong")
    private ResponseResult UserStudentChong(@Param("id") Integer id) {
        return userStudentSerivice.UserStudentChong(id);
    }



        //管理端课件学习详情
    @RequestMapping("/xuedetail")
    private ResponseResult CoursewareXuedetail(UserStudentXUedetaile queryware) {
        return userStudentSerivice.UserStudentXuedetail(queryware);
    }

    //管理端的学习数据概况
    @RequestMapping("/xueCount")
    private ResponseResult CoursewareXuedetailTong(@Param("id") Integer id) {
        return userStudentSerivice.UserStudentXueCount(id);
    }


    //数据跟新
//    @RequestMapping("/xueCount")
//    private ResponseResult CoursewareXuedetailTong(@Param("id") Integer id) {
//        return userStudentSerivice.UserStudentXueCount(id);
//    }


    //管理端添加
    @RequestMapping("/save")
    private ResponseResult CoursewareSave(@RequestBody @Validated UserStudentAdd userStudentAdd) {
        return userStudentSerivice.UserStudentXueSave(userStudentAdd);
    }

    //管理端编辑
    @RequestMapping("/UpdateById")
    private ResponseResult CoursewareByod(@Param("id") Integer id) {
        return userStudentSerivice.UserStudentByid(id);
    }


    @RequestMapping("/getTestCode")
    private ResponseResult getTestCodeYan(@Param("phone") String phone) {
        return userStudentSerivice.StugetTestCodeYan(phone);
    }



//设备端学生 基本信息
    @RequestMapping("/jinBen")
    private ResponseResult TeadetailJinBan(@Param("id") Integer id, HttpServletRequest request) {
        return userStudentSerivice.StudentJinBenDetail(id,request);
    }

//设备端学生 修改 密码
    @RequestMapping("/xiuGai")
    private ResponseResult TeaXiuGai(@RequestBody  @Validated StuXiuGai teaXiuGai) {
        return userStudentSerivice.XiuGaiTead(teaXiuGai);
    }


    //设备端返回报告数据
    @RequestMapping("/addBao")
    private  ResponseResult  addBaoGao(@RequestBody SheBao sheBao){
        return userStudentSerivice.BaoGao(sheBao);
    }

    //限制
    @RequestMapping("/UpdateShe")
    private ResponseResult InstitutioxianShe(@Param("id") Integer id){
        return    userStudentSerivice.UpdateSheXian(id);
    }

    //接触限制
    @RequestMapping("/contact")
    private ResponseResult  StudentContact(@Param("id") Integer id){
        return    userStudentSerivice.Studentcontact(id);
    }
    //多选接触限制
    @RequestMapping("/mcontact")
    private ResponseResult  Studentmultiple(@Param("ide") String  ide){
        return    userStudentSerivice.Studentmultiple(ide);
    }

    //全选限制A
    @RequestMapping("/strict")
    private ResponseResult Institutioxian(@Param("ide") String ide){
        return    userStudentSerivice.QuanXian(ide);
    }

//设备端学生
    @RequestMapping("/xiuGaiPhone")
    private ResponseResult TeaXiuGaiPhne(@RequestBody @Validated TeaXiuGai teaXiuGai) {
        return userStudentSerivice.XiuGaiTeadPhone(teaXiuGai);
    }

    //公众号修改学生姓名
    @RequestMapping("/reality")
    private ResponseResult reality(@RequestBody @Validated  UserStudentAccount userStudentAccount){
        return userStudentSerivice.XiuGaiReality(userStudentAccount);
    }


    @RequestMapping("/Daochu")
    private ResponseResult StuDaochu(HttpServletRequest request, HttpServletResponse response) {
        return userStudentSerivice.Daochu(request, response);
    }

    @RequestMapping("/import")
    public ResponseResult StuImprot(HttpServletRequest request, HttpServletResponse response, MultipartFile file) throws Exception {
        return userStudentSerivice.improtStu(request, response, file);
    }

    //管理端导入数据确认
    @RequestMapping("/importAdd")
    private ResponseResult CoursewareAdd(@RequestBody UserStudentImporAdd  userStudentAdd) {
        return userStudentSerivice.UserStudentAdd(userStudentAdd);
    }




}